package com.zaaach.citypicker.adapter;


import com.zaaach.citypicker.ResourceTable;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;
import ohos.agp.components.*;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.List;

/**
 * @Author: Bro0cL
 * @Date: 2018/2/5 12:06
 */
public class SearchCityListAdapter extends BaseItemProvider {
    private Context mContext;
    private List<City> mData;
    private List<HotCity> mHotData;
    private int locateState;
    private InnerListener mInnerListener;


    public SearchCityListAdapter(List<City> list, List<HotCity> hotData, int state, Context slice) {
        this.mData = list;
        this.mContext = slice;
        this.mHotData = hotData;
        this.locateState = state;
    }


    // 用于保存列表项中的子组件信息
    public static class DefaultViewHolder {
        Text name;
        StackLayout header;
        Text headerText;

        DefaultViewHolder(Component itemView) {
            header = (StackLayout) itemView.findComponentById(ResourceTable.Id_header_layout);
            headerText = (Text) itemView.findComponentById(ResourceTable.Id_header);
            name = (Text) itemView.findComponentById(ResourceTable.Id_cp_list_item_name);
        }
    }


    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (mData != null && position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        DefaultViewHolder holder3 = null;
        City data = mData.get(position);
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_cp_list_item_default_layout, null, false);
            holder3 = new DefaultViewHolder(component);
            component.setTag(holder3);
        } else {
            holder3 = (DefaultViewHolder) component.getTag();
        }

        if (position>0&&TextTool.isEqual(data.getPinyin().substring(0, 1), mData.get(position - 1).getPinyin().substring(0, 1))) {
            holder3.header.setVisibility(Component.HIDE);
        } else {
            holder3.header.setVisibility(Component.VISIBLE);
        }
        holder3.headerText.setText(data.getPinyin().substring(0, 1).toUpperCase());
        holder3.name.setText(data.getName());
        holder3.name.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mInnerListener != null) {
                    mInnerListener.dismiss(position, data);
                }
            }
        });


        return component;
    }

    public void setInnerListener(InnerListener listener) {
        this.mInnerListener = listener;
    }
}

